/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.college;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;
import jobportal.EmailSender;

/**
 *
 * @author Ravindra
 */
public class CollegeEmailVerification extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("verify.jsp");
        String to = "", subject = "Email Verification code", msg = "Your Email Verification code ::";
        try {
            Connection con = Dbconn.getConn();
            Statement st = con.createStatement();
            HttpSession session = request.getSession();
            String vercode = "";
            if (request.getParameter("em_code") != null) {
                vercode = request.getParameter("em_code");
            }
            ResultSet rs = st.executeQuery("select tpoemail,emailstatus from college where clgid='" + session.getAttribute("username") + "'");
            String emailstatus = "";

            if (rs.next()) {
                emailstatus = rs.getString(1);
                to = rs.getString("tpoemail");
            }
            String sql = "";
            if (emailstatus.equalsIgnoreCase("verified")) {
                request.setAttribute("status", "Email Already Verified");
                  rd.forward(request, response);
            }
            if (request.getParameter("Submit").equals("Verify Now")) {
                sql = "update college set emailstatus='Verified' where clgid='" + session.getAttribute("username") + "' and emailstatus='" + vercode + "'";
                out.println(sql);
                if (st.executeUpdate(sql) > 0) {
                     request.setAttribute("status", "<font color='green' >Email Verified</font>");
                    out.print("verified");
                } else {
                    request.setAttribute("status", "<font color='red'>Invalid verification code</font>");
                    out.println("not verifed");
                }
              //  out.println("inside verifi");
            } else if (request.getParameter("Submit").equals("newverf")) {
                Random r = new Random();
              Long i = r.nextLong();
              if(i<0)
                  i*=-1;
                msg += i;
                sql = "update college set emailstatus='" + i + "' where clgid='" + session.getAttribute("username") + "'";

                st.executeUpdate(sql);
                EmailSender.sendMail(to, subject, msg);
                request.setAttribute("status", "Verification Code is Sent to your registered mail<br>Please Check your mail to verify emailid");
            }
            rd.forward(request, response);
        } catch (Exception e) {
            out.println(e);
            rd.forward(request, response);
        } finally {
            out.close();

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
